package com.alianzamedica.controllers.profile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.objectsearch.sqlsearch.ObjectSearch;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Profile;
import com.alianzamedica.tools.Enviroment;
import com.alianzamedica.view.ProfileSaveForm;

/**
 * @author Carlos
 * 
 */
public class ProfileSaveAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProfileSaveForm psf = (ProfileSaveForm) form;
		Profile profile = psf.getObjectProfile();
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		String mensaje = "";
		if (profile.getId() == null) {
			search.insertObject(profile);
			mensaje = "El objeto perfil se ha agregado correctamente";
		} else {
			search.updateObject(profile);
			mensaje = "El objeto perfil se ha actualizado correctamente";
		}
		String linkRetorno = "/admin/profile/list.do";
		request.setAttribute("mensaje", mensaje);
		request.setAttribute("linkRetorno", linkRetorno);
		return mapping.findForward("success");
	}

}
